package com.blb.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Order;
import com.blb.entity.Product;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.entity.Product;
import com.blb.entity.Relation;
import com.blb.entity.User;
import com.blb.entity.WareMenu;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.IOrderService;
import com.blb.service.IProductService;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/buy")
public class BuyerController {

    private static User user;

    @Resource
    private IProductService productService;
    @Resource
    private IOrderService orderService;

    @RequiresPermissions("shopping_into")
    @RequestMapping("into")
    public String index(){
        user=ShiroUtils.getUser();
        return "buyer/online_order_xd";
    }

    @RequiresPermissions("shopping_into")
    @RequestMapping("intoDetail")
    public String intoDetail(Model model,String productId){
        Map<String, Object> product = productService.getProductById(productId);
        int monthSale = productService.monthSale(productId);
        product.put("monthSale",monthSale);
        model.addAttribute("product",product);
        return "buyer/online_order_xd_detail";
    }

    @RequiresPermissions("shopping_into")
    @ResponseBody
    @PostMapping("page")
    public BlbPage page(int start, int length,String nodeLevel,String menuId){
        List<Map<String, Object>> list;
        int count;
        switch (nodeLevel) {
            case "0":
                list = productService.getPBySupplier(menuId,start, length);
                count = productService.countGetPBySupplier(menuId);
                break;
            case "1":
            case "2":
                list = productService.getPByEnterprise(menuId,start, length);
                count = productService.countGetPByEnterprise(menuId);
                break;
            default:
                //查询分页数据
                list = productService.getPByCompany(user.getCompanyId(), start, length);
                //查询总条数
                count = productService.countGetPByCompany(user.getCompanyId());
                break;
        }
        for (Map<String, Object> map:list) {
            int stock = productService.getStockById((String) map.get("id"));
            map.put("stock",stock);
        }
        return new DataTablePage(count, count, list);
    }

    @RequiresPermissions("shopping_into")
    @ResponseBody
    @PostMapping("buy")
    public ResponseHelp buy(Order order){
        User user = ShiroUtils.getUser();
        Product product = productService.getOne(new QueryWrapper<Product>().eq("id", order.getProductId()));
        order.setId(UUIDUtils.getId());
        order.setOrderType("线上支付");
        order.setCustomId(user.getCompanyId());
        order.setPayType("银联卡");
        order.setOrderStatus("1");
        order.setDealTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        order.setSupplierId(product.getCompanyId());
        order.setOrderHolder(user.getUserName());
        System.out.println(order);
        orderService.save(order);
        return ResponseHelp.success();
    }
    /**
     * 跳转到采购申请列表
     * @return
     * xuyuanxin
     */

    @RequiresPermissions("order_permission")
    @RequestMapping("order")
    public String orderCx(){
        return "buyer/Order_cx";
    }
    /**
     * 跳转到采购申请列表--详细信息
     * @return
     * xuyuanxin
     */
    @RequestMapping("orderdetial")
    public String orderdetail(Model model,String orderId){
        User user = ShiroUtils.getUser();
        Product detailOrder = productService.findDetailOrder(user.getCompanyId(), orderId);
        model.addAttribute("detailOrder",detailOrder);
        return "buyer/or_xiangqing";
    }
    /**
     * 跳转到采购申请列表--物流信息
     * @return
     * xuyuanxin
     */
    @RequestMapping("orderWdetial")
    public String orderdetail1(Model model,String orderId){
        User user = ShiroUtils.getUser();
        Product logistics = productService.findLogistics(user.getCompanyId(), orderId);
        String orderStatus = logistics.getOrder().getOrderStatus();
        System.out.println(orderStatus);
        model.addAttribute("orderStatus",orderStatus);
        model.addAttribute("logistics",logistics);
        return "buyer/order_wl";
    }

    /**
     * 采购申请列表首页
     * @param start
     * @param length
     * @param orderId
     * @param pName
     * @param orderStatus
     * @param dealTime
     * @return
     * xuyuanxin
     */
    @ResponseBody
    @PostMapping("orderlist")
    public BlbPage page(int start, int length, String orderId, String pName, String orderStatus, String dealTime){
        User user = ShiroUtils.getUser();
        List<Product> allOrder = productService.findAllOrder(start, length, user.getCompanyId(), orderId, pName, orderStatus, dealTime);
        System.out.println(allOrder);
        int count = productService.countOrder(user.getCompanyId(), orderId, pName, orderStatus, dealTime);

        return new DataTablePage(count, count, allOrder);
    }

    /**
     * 三级菜单
     * @return
     */
    @ResponseBody
    @RequestMapping("menus")
    public List<WareMenu> menus(){
        return productService.getMenusByCompanyId(user.getCompanyId());
    }
}
